/**
 * 
 */
package com.usc.entme;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Dhrumil
 *
 */
/*public class ListItemArrayAdapter extends ArrayAdapter<ListItem> {

    private ArrayList<ListItem> items;
    LayoutInflater vi;
    public ListItemArrayAdapter(Context context, int textViewResourceId, ArrayList<ListItem> items) {
            super(context, textViewResourceId, items);
            vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            ListItem o = items.get(position);
            if (o != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                    if (tt != null) {
                          tt.setText("Name: "+o.getOrderName());                            }
                    if(bt != null){
                          bt.setText("Status: "+ o.getOrderStatus());
                    }
            }
            return v;
    }
}

*/

public class ListItemArrayAdapter extends ArrayAdapter<ListItem>{
	int resource;
	private String TAG = "Message";
	LayoutInflater vi;
	private List<ListItem> items;
	private Bitmap bmImg;

	public ListItemArrayAdapter(Context context, int _resource, List<ListItem> listitems) {
		super(context, _resource, listitems);
		Log.e( TAG ,"listItemArrayAdapter");
		vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		resource = _resource;
		this.items = listitems;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d(TAG, "listItemArrayAdapter.getView");
		LinearLayout newView;

		ListItem item = getItem(position);

		if (convertView == null) {
			newView = new LinearLayout(getContext());
			vi.inflate(R.layout.row, newView);
		} else {
			newView = (LinearLayout)convertView;
		}
		
		TextView textview = (TextView)newView.findViewById(R.id.newstitle);
		
		Log.d(TAG, item.title);
		textview.setText(item.title);
		
		ImageView imgView = (ImageView)newView.findViewById(R.id.newsimage);
		/*Log.d(TAG, item.imgUrl.toString());
		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection)item.imgUrl.openConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 conn.setDoInput(true);
		 try {
			conn.connect();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 int length = conn.getContentLength();
		 int[] bitmapData =new int[length];
		 byte[] bitmapData2 =new byte[length];
		 InputStream is = null;
		try {
			is = conn.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 bmImg = BitmapFactory.decodeStream(is);*/
		 imgView.setImageBitmap(item.imgBitMap);
		 

		 return newView;	 
	}

}
